//
//  CYURLUtils.m
//  MeiJianApp
//
//  Created by 志刚杨 on 2017/11/8.
//  Copyright © 2017年 voidcat. All rights reserved.
//

#import "CYURLUtils.h"

@implementation CYURLUtils
SINGLETON_FOR_CLASS(CYURLUtils)
- (NSString *)keyValueStringWithDict:(NSDictionary *)dict
{
    if (dict == nil) {
        return nil;
    }
    NSMutableString *string = [NSMutableString stringWithString:@"?"];
    [dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        [string appendFormat:@"%@=%@&",key,obj];
    }];
    
    if ([string rangeOfString:@"&"].length) {
        [string deleteCharactersInRange:NSMakeRange(string.length - 1, 1)];
    }
    
    return string;
}

-(NSDictionary *)dictionaryWithUrlString:(NSString *)urlStr
{
    if (urlStr && urlStr.length && [urlStr rangeOfString:@"?"].length == 1) {
        NSArray *array = [urlStr componentsSeparatedByString:@"?"];
        if (array && array.count == 2) {
            NSString *paramsStr = array[1];
            if (paramsStr.length) {
                NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
                NSArray *paramArray = [paramsStr componentsSeparatedByString:@"&"];
                for (NSString *param in paramArray) {
                    if (param && param.length) {
                        NSArray *parArr = [param componentsSeparatedByString:@"="];
                        if (parArr.count == 2) {
                            [paramsDict setObject:parArr[1] forKey:parArr[0]];
                        }
                    }
                }
                return paramsDict;
            }else{
                return nil;
            }
        }else{
            return nil;
        }
    }else{
        return nil;
    }
}

-(id)safeUrl:(NSString *)url
{
    if([url hasPrefix:@"http"])
    {
        return [NSURL URLWithString:url];
    }
    else
    {
        //兼容Windows服务器上传图片时的非法错误路径
        url = [url stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
        NSLog(@"%@",[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",URL_main,url]]);
        return [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",URL_main,url]];
    }

}


-(NSString *)makeURLWithC:(NSString *)c A:(NSString *)a;
{
//    return NSStringFormat(@"%@index.php/mapi/public/index.php/index/%@/%@",URL_main,c,a);
    return NSStringFormat(@"%@/mapi/public/index.php/api/%@/%@",URL_main,c,a);

}
@end
